
package dynamicdiscountsystem;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


public class DiscountApplication extends javax.swing.JInternalFrame {

    /** Creates new form DiscountApplication */
    public DiscountApplication() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonRight = new javax.swing.JButton();
        jButtonLeft = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jButton1Add = new javax.swing.JButton();
        jButtonUpdate = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        setBorder(new javax.swing.border.MatteBorder(null));
        setIconifiable(true);
        setTitle("Endirimlərin Tətbiqi");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                formİnternalFrameActivated(evt);
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonRight.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/right.png"))); // NOI18N
        jButtonRight.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jButtonRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRightActionPerformed(evt);
            }
        });

        jButtonLeft.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/left.png"))); // NOI18N
        jButtonLeft.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jButtonLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLeftActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Ümumi Malların Siyahısı", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Malın Adı", "Malın Növü", "Malın Sayı", "Malın Alış Qiyməti", "Malın Satış Qiyməti"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 577, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Endirimli Malların Siyahısı", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jTable2.setAutoCreateRowSorter(true);
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Malın Adı", "Malın Növü", "Malın Sayı", "Malın Alış Qiyməti", "Malın Satış Qiyməti"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 577, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonRight, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jButtonRight)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonLeft))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(153, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, 1270, 640));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Əməliyyatlar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jButton1Add.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton1Add.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/save.png"))); // NOI18N
        jButton1Add.setText("Əlavə Et");
        jButton1Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1AddActionPerformed(evt);
            }
        });

        jButtonUpdate.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButtonUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/update.png"))); // NOI18N
        jButtonUpdate.setText("Siyahını yenilə");
        jButtonUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jButton1Add, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jButtonUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1Add, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 10, -1, -1));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Endirim Növü", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("Endirimin Növü :");

        jComboBox1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1. Fərdi Endirimlər", "2. Məbləğ Endirimi", "3. N al M ödə Endirimi" }));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void updateFrame() throws SQLException {
        DatabaseConnection connect = new DatabaseConnection();

        try {
            DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
            DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();
            while (model1.getRowCount() > 0) {
                model1.removeRow(0);
            }
            while (model2.getRowCount() > 0) {
                model2.removeRow(0);
            }

            String command = "SELECT * FROM PRODUCT_INPUT ORDER BY PRODUCT_NAME,PRODUCT_MODEL ASC";
            ResultSet rs = connect.ExecuteQuery(command);
            while (rs.next()) {

                Integer idPRODUCT_INPUT = rs.getInt("idPRODUCT_INPUT");
                String PRODUCT_NAME = rs.getString("PRODUCT_NAME");
                String PRODUCT_MODEL = rs.getString("PRODUCT_MODEL");
                Integer PRODUCT_AMOUNT = rs.getInt("PRODUCT_AMOUNT");
                Double PRODUCT_PURCHASE_COST = rs.getDouble("PRODUCT_PURCHASE_COST");
                Double PRODUCT_SALE_COST = rs.getDouble("PRODUCT_SALE_COST");
                Object[] newRow = new Object[]{idPRODUCT_INPUT, PRODUCT_NAME, PRODUCT_MODEL, PRODUCT_AMOUNT, PRODUCT_PURCHASE_COST, PRODUCT_SALE_COST};
                model1.addRow(newRow);
            }
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null,
                    "Xəta baş verdi! \nXəta mesajı = " + ex.getMessage(),
                    "XƏTA", JOptionPane.ERROR_MESSAGE);
        }
    }
    private void formİnternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formİnternalFrameActivated

        try {
            updateFrame();
        } catch (SQLException ex) {
            Logger.getLogger(DiscountApplication.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formİnternalFrameActivated

    private void jButtonRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRightActionPerformed

        int rowCount = jTable1.getSelectedRowCount();
        DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
        DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();

        if (rowCount > 0) {
            int[] selectedRows = jTable1.getSelectedRows();
            int selectedRow = 0;
            int count = 0;
            for (int i = 0; i < rowCount; i++) {

                selectedRow = selectedRows[i] - count;
                //JOptionPane.showMessageDialog(null, model1.getValueAt(selectedRow, 0)+"   "+selectedRow);
                Integer idPRODUCT_INPUT = (Integer) model1.getValueAt(selectedRow, 0);
                String PRODUCT_NAME = (String) model1.getValueAt(selectedRow, 1);
                String PRODUCT_MODEL = (String) model1.getValueAt(selectedRow, 2);
                Integer PRODUCT_AMOUNT = (Integer) model1.getValueAt(selectedRow, 3);
                Double PRODUCT_PURCHASE_COST = (Double) model1.getValueAt(selectedRow, 4);
                Double PRODUCT_SALE_COST = (Double) model1.getValueAt(selectedRow, 5);
                Object[] newRow = new Object[]{idPRODUCT_INPUT, PRODUCT_NAME, PRODUCT_MODEL, PRODUCT_AMOUNT, PRODUCT_PURCHASE_COST, PRODUCT_SALE_COST};
                model2.addRow(newRow);
                model1.removeRow(selectedRow);
                count++;
            }

        }

    }//GEN-LAST:event_jButtonRightActionPerformed

    private void jButtonUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpdateActionPerformed
        try {
            updateFrame();
        } catch (SQLException ex) {
            Logger.getLogger(DiscountApplication.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButtonUpdateActionPerformed

    private void jButtonLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLeftActionPerformed

        int rowCount = jTable2.getSelectedRowCount();
        DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
        DefaultTableModel model2 = (DefaultTableModel) jTable2.getModel();

        if (rowCount > 0) {
            int[] selectedRows = jTable2.getSelectedRows();
            int selectedRow = 0;
            int count = 0;
            for (int i = 0; i < rowCount; i++) {

                selectedRow = selectedRows[i] - count;
                //JOptionPane.showMessageDialog(null, model1.getValueAt(selectedRow, 0)+"   "+selectedRow);
                Integer idPRODUCT_INPUT = (Integer) model2.getValueAt(selectedRow, 0);
                String PRODUCT_NAME = (String) model2.getValueAt(selectedRow, 1);
                String PRODUCT_MODEL = (String) model2.getValueAt(selectedRow, 2);
                Integer PRODUCT_AMOUNT = (Integer) model2.getValueAt(selectedRow, 3);
                Double PRODUCT_PURCHASE_COST = (Double) model2.getValueAt(selectedRow, 4);
                Double PRODUCT_SALE_COST = (Double) model2.getValueAt(selectedRow, 5);
                Object[] newRow = new Object[]{idPRODUCT_INPUT, PRODUCT_NAME, PRODUCT_MODEL, PRODUCT_AMOUNT, PRODUCT_PURCHASE_COST, PRODUCT_SALE_COST};
                model1.addRow(newRow);
                model2.removeRow(selectedRow);
                count++;
            }

        }
    }//GEN-LAST:event_jButtonLeftActionPerformed

    private void jButton1AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1AddActionPerformed

        int rowCount = jTable2.getRowCount();
        if (rowCount > 0) {
            DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
            DatabaseConnection con = new DatabaseConnection();
            
            for (int i = 0; i < rowCount; i++) {

                int productIdNumber = (Integer) model.getValueAt(i, 0);
                int discountNumber = jComboBox1.getSelectedIndex()+1;
                String command = "INSERT INTO DISCOUNT_APPLICATION "
                            + "(idPRODUCT_INPUT,DISCOUNT_NUMBER) VALUES "
                            + "(" + productIdNumber + "," + discountNumber + ")";
                //JOptionPane.showMessageDialog(this, command);
                try {
                    con.Execute(command);
                    
                } catch (Exception ex) {
                    Logger.getLogger(DiscountApplication.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }

    }//GEN-LAST:event_jButton1AddActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1Add;
    private javax.swing.JButton jButtonLeft;
    private javax.swing.JButton jButtonRight;
    private javax.swing.JButton jButtonUpdate;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables
}
